<?php
/**
 * 商家会员卡订单
 * User: ws
 * Date: 2018/10/19
 * Time: 18:24
 */

namespace app\merchant\controller;

use think\Db;
class Card  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['s.store_id'] = $_SESSION['think']['admin_user']['store_id'];

            $list = Db::table('je_store_user')->alias('su')->join('je_store s','su.store_id = s.store_id')->field('su.name as suname,s.name as sname,su.sn') ->where($where)->select();

            $list = array_column($list,'sn');
            $where['c.store_user_sn'] = array('in',$list);



            if(is_numeric(input('type')) === true){
                $where['m.type'] = input('type');
            }

            if(is_numeric(input('store_name')) === true){
                $where['c.store_user_sn'] = input('store_name');
            }
            $where['c.card_order_sn|u.name|u.phone'] = array('like','%'.trim($key).'%');

            unset($where['s.store_id']);
            unset($list);



            $list = Db::table('je_card_order')
                ->alias('c')->join('je_membership_card m','c.card_id = m.card_id')
                ->join('je_user u','c.user_id = u.user_id')
                ->field('u.name as uname,u.phone,m.type as stype,c.*,m.name mname')->order('card_order_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$item){
                $list['data'][$key]['store_user_sn'] = db('store_user')->where('sn',$item['store_user_sn'])->value('name');
                $list['data'][$key]['pay_time'] = date('Y-m-d',$item['pay_time']);
                $list['data'][$key]['validity_time'] = date('Y-m-d',$item['validity_time']);

            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        $storeName = db('store_user')->field('name,sn')->where("store_id={$_SESSION['think']['admin_user']['store_id']}")->select();

        return view('list',['list'=>$storeName]);
    }

}